package cn.bluethink.view;

import cn.bluethink.MapFrame;
import cn.bluethink.graphical.Polygon;
import cn.bluethink.graphical.Polyline;
import cn.bluethink.mouse.IMouseOperator;
import cn.bluethink.mouse.MouseDrawLineOp;
import cn.bluethink.mouse.MouseDrawPolylineOp;
import cn.bluethink.mouse.MouseDrowPolygonOp;
import cn.bluethink.utile.*;
import org.springframework.util.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BrowseView{

    private Map<String,String> nodesInfo;

    public void browseGraphical() {

        for (Map.Entry<String,String> entry : nodesInfo.entrySet()){

            JFrame jFrame = new JFrame();
            final GraphView canvas = new GraphView();

            JToolBar toolBar = new JToolBar();
            toolBar.setFont(new Font("宋体", Font.PLAIN, 24));
            jFrame.getContentPane().add(toolBar, BorderLayout.NORTH);

            JToolBar toolBarGraph = new JToolBar();
            toolBar.add(toolBarGraph);

            JButton editButton = new JButton();
            editButton.setToolTipText("编辑");
            editButton.setIcon(new ImageIcon(MapFrame.class.getResource("/res/editor.gif")));
            toolBarGraph.add(editButton);
            editButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    canvas.mCurrentOp = "select";
                }
            });

            JButton saveButton = new JButton();
            saveButton.setToolTipText("保存");
            saveButton.setIcon(new ImageIcon(MapFrame.class.getResource("/res/folder.gif")));
            toolBarGraph.add(saveButton);


            jFrame.setVisible(true);
            jFrame.setBounds(100, 100, 450, 300);
            jFrame.getContentPane().add(canvas, BorderLayout.CENTER);
            jFrame.addComponentListener(new ComponentAdapter(){
                @Override public void componentResized(ComponentEvent e){
                    canvas.onSizeChanged();
                }});

            if (entry.getKey().startsWith("polyline")){

                final String id = entry.getKey().substring(8);
                final Graph polyline = GraphicalUtil.getGraphByType(entry.getValue(),"polyline");

                canvas.mGraphs.add(polyline);
                canvas.mMoustOperator.put("polyline",new MouseDrawPolylineOp(canvas));

                saveButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            GraphicalUtil.saveGraphInfo("update",id,polyline);
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            }else if (entry.getKey().startsWith("polygon")){

                final String id = entry.getKey().substring(7);
                final Graph polygon = GraphicalUtil.getGraphByType(entry.getValue(),"polygon");

                canvas.mGraphs.add(polygon);
                canvas.mMoustOperator.put("polygon",new MouseDrowPolygonOp(canvas));

                saveButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            GraphicalUtil.saveGraphInfo("update",id,polygon);
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            }else if (entry.getKey().startsWith("line")){
                final String id = entry.getKey().substring(5);
                final Graph line = GraphicalUtil.getGraphByType(entry.getValue(),"line");

                canvas.mGraphs.add(line);
                canvas.mMoustOperator.put("line",new MouseDrawLineOp(canvas));

                saveButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            GraphicalUtil.saveGraphInfo("update",id,line);
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            }

        }
    }

    public static void showGraphical(){
        BrowseView browseView = new BrowseView();
        browseView.setNodesInfo(getGraphicalInfo());
        browseView.browseGraphical();
    }

    /**发起http请求Graphical类相关信息 key:type + id value:[(x,y)...]
     * @return
     */
    private static Map<String,String> getGraphicalInfo(){
        String res = null;
        try {
            res = HttpUtil.sendGet("http://localhost/graphical/info","userId=" + AuthenticationUtil.userId);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String nodesInfo = JsonUtil.fastJson(res,"data");
        return JsonUtil.getJson(nodesInfo,"type");
    }

    public Map<String, String> getNodesInfo() {
        return nodesInfo;
    }

    public void setNodesInfo(Map<String, String> nodesInfo) {
        this.nodesInfo = nodesInfo;
    }

}
